package cn.simon.controller;


import cn.simon.pojo.User;
import cn.simon.service.Impl.GoodsServiceImpl;
import cn.simon.vo.DetailVo;
import cn.simon.vo.GoodsVo;
import cn.simon.vo.RespBean;
import cn.simon.vo.RespBeanEnum;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 秒杀商品表 前端控制器
 * </p>
 * <p>
 * <p>
 * simon-yian 开心
 *
 * @author simon-yian
 */
@Log
@Controller
@RequestMapping("")
public class SeckillGoodsController {

    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

   /**
    * @功能描述: 商品详情：使用 “页面缓存”
    * @Param:
    * @Return:
    * @Author simon-yi'an
    * @Date 2022/11/2 15:15
    */
    @RequestMapping(value = "/goodsDetail1", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String goodDetail1(Model model, User user, Long goodsId,
                             HttpServletRequest request, HttpServletResponse response) {

        //判断该商品页面是否在缓存中
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsDetail?goodsId=" + goodsId);
        if (!StringUtils.isEmpty(html)) {
            return html;
        }

        GoodsVo goodsVo = (GoodsVo) goodsService.findGoodsVoById(goodsId);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        log.info(nowDate.toString() + "-----------------");
        //秒杀状态
        int secKillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            remainSeconds = ((int) ((startDate.getTime() - nowDate.getTime()) / 1000));
        } else if (nowDate.after(endDate)) {
            //	秒杀已结束
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀中
            secKillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("remainSeconds", remainSeconds);
        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("goods", goodsVo);
        model.addAttribute("user", user);

        WebContext webContext = new WebContext(request, response,
                request.getServletContext(), request.getLocale(),
                model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);
        if (!StringUtils.isEmpty(html)) {
            valueOperations.set("goodsDetail?goodsId=" + goodsId, html, 60, TimeUnit.SECONDS);
        }
        return html;
    }

}
